define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'hd/forum/type/index' + location.search,
                    add_url: 'hd/forum/type/add',
                    edit_url: 'hd/forum/type/edit',
                    del_url: 'hd/forum/type/del',
                    multi_url: 'hd/forum/type/multi',
                    import_url: 'hd/forum/type/import',
                    table: 'hd_forum_type',
                }
            });
            this.table.first();
            var maxLevel = Config['maxLevel'];
            if (maxLevel == 2 || maxLevel == 3) {
                this.table.second();
            }
            if (maxLevel == 3) {
                this.table.three();
            }
        },
        table: {
            first: function () {
                // 表格1
                var table1 = $("#table1");
                table1.bootstrapTable({
                    url: 'hd/forum/type/table1',
                    toolbar: '#toolbar1',
                    sortName: 'id',
                    search: false,
                    // pk: 'id',
                    // sortName: 'id',
                    // fixedColumns: true,
                    // fixedRightNumber: 1,
                    columns: [
                        [
                            { checkbox: true },
                            { field: 'id', title: __('Id') },
                            { field: 'image', title: __('Image'), operate: false, events: Table.api.events.image, formatter: Table.api.formatter.image },
                            { field: 'name', title: __('Name'), operate: 'LIKE', table: table1, class: 'autocontent', formatter: Table.api.formatter.content },
                            // { field: 'level', title: __('Level') },
                            // { field: 'superior_id', title: __('Superior_id') },

                            // { field: 'create_time', title: __('Create_time'), operate: 'RANGE', addclass: 'datetimerange', autocomplete: false, formatter: Table.api.formatter.datetime },
                            // { field: 'update_time', title: __('Update_time'), operate: 'RANGE', addclass: 'datetimerange', autocomplete: false, formatter: Table.api.formatter.datetime },
                            {
                                field: 'operate', title: __('Operate'), table: table1, events: Table.api.events.operate, buttons: (Config['maxLevel'] == 2 || Config['maxLevel'] == 3) ? [
                                    {
                                        name: 'log',
                                        title: '下级类别',
                                        text: '下级类别',
                                        icon: 'fa fa-list',
                                        classname: 'btn btn-primary btn-xs btn-click',
                                        click: function (e, data) {
                                            console.log(Config['maxLevel']);
                                            console.log(data)
                                            $("#myTabContent2 .form-commonsearch input[name='superior_id']").val(data.id);
                                            $("#myTabContent2 .btn-refresh").trigger("click");
                                        }
                                    }
                                ] : [],
                                formatter: Table.api.formatter.operate
                            }
                        ]
                    ]
                });

                // 为表格1绑定事件
                Table.api.bindevent(table1);
            },

            second: function () {
                // 表格2
                var table2 = $("#table2");
                table2.bootstrapTable({
                    url: 'hd/forum/type/table2',
                    toolbar: '#toolbar2',
                    sortName: 'id',
                    search: false,
                    columns: [
                        [
                            { checkbox: true },
                            { field: 'id', title: __('Id') },
                            { field: 'image', title: __('Image'), operate: false, events: Table.api.events.image, formatter: Table.api.formatter.image },
                            { field: 'name', title: __('Name'), operate: 'LIKE', table: table2, class: 'autocontent', formatter: Table.api.formatter.content },
                            // { field: 'level', title: __('Level') },
                            { field: 'superior_id', title: __('Superior_id'), visible: false },

                            // { field: 'create_time', title: __('Create_time'), operate: 'RANGE', addclass: 'datetimerange', autocomplete: false, formatter: Table.api.formatter.datetime },
                            // { field: 'update_time', title: __('Update_time'), operate: 'RANGE', addclass: 'datetimerange', autocomplete: false, formatter: Table.api.formatter.datetime },
                            {
                                field: 'operate', title: __('Operate'), table: table2, events: Table.api.events.operate, buttons: (Config['maxLevel'] == 3) ? [
                                    {
                                        name: 'log',
                                        title: '下级类别',
                                        text: '下级类别',
                                        icon: 'fa fa-list',
                                        classname: 'btn btn-primary btn-xs btn-click',
                                        click: function (e, data) {
                                            console.log(Config['maxLevel']);
                                            console.log(data)
                                            $("#myTabContent3 .form-commonsearch input[name='superior_id']").val(data.id);
                                            $("#myTabContent3 .btn-refresh").trigger("click");
                                        }
                                    }
                                ] : [],
                                formatter: Table.api.formatter.operate
                            }
                        ]
                    ]
                });

                // 为表格2绑定事件
                Table.api.bindevent(table2);
            },
            three: function () {
                // 表格2
                var table3 = $("#table3");
                table3.bootstrapTable({
                    url: 'hd/forum/type/table3',
                    toolbar: '#toolbar3',
                    sortName: 'id',
                    search: false,
                    columns: [
                        [
                            { checkbox: true },
                            { field: 'id', title: __('Id') },
                            { field: 'image', title: __('Image'), operate: false, events: Table.api.events.image, formatter: Table.api.formatter.image },
                            { field: 'name', title: __('Name'), operate: 'LIKE', table: table2, class: 'autocontent', formatter: Table.api.formatter.content },
                            // { field: 'level', title: __('Level') },
                            // { field: 'superior_id', title: __('Superior_id') },

                            // { field: 'create_time', title: __('Create_time'), operate: 'RANGE', addclass: 'datetimerange', autocomplete: false, formatter: Table.api.formatter.datetime },
                            // { field: 'update_time', title: __('Update_time'), operate: 'RANGE', addclass: 'datetimerange', autocomplete: false, formatter: Table.api.formatter.datetime },
                            { field: 'operate', title: __('Operate'), table: table3, events: Table.api.events.operate, formatter: Table.api.formatter.operate }
                        ]
                    ]
                });

                // 为表格2绑定事件
                Table.api.bindevent(table3);
            }
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
